package com.xiaoy.util.page;

import java.util.Enumeration;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springside.modules.orm.PropertyFilter;

import com.xiaoy.util.store.Templatekeys;

public class PageParamUtil {

	
	/**
	 * 获得表单的信息
	 * @param request
	 * @return
	 */
	public static FormInfo getFormInfo (HttpServletRequest request) {
		
		// form_tablename_processPrama_field
		String tablename = request.getParameter(Templatekeys.pageForm.TABLENAME);
		
		Map<String,Object>formParamMap = getParametersStartingWith(request,tablename+"_");
		
		FormInfo formInfo = new FormInfo();
		formInfo.setTablename(tablename);
		for (Map.Entry<String, Object> entry : formParamMap.entrySet()) {
			String key = entry.getKey();
			Object value = entry.getValue();
			
			String isPp = key.substring(0,Templatekeys.pageForm.ISPROCESSPARAM.length());
			
			if (Templatekeys.pageForm.ISPROCESSPARAM.equals(isPp)) {
				key = key.substring(isPp.length()+1);
				formInfo.addProcessParams(key, value);
			}
			formInfo.addFormFields(key, value);
			
//			PropertyField propertyField = new PropertyField(key,value);
			
//			String processParam = key.split("_")[0];
//			String field = key.substring(tablename.length()+1);	//流程字段名
//			
//			formInfo.addFormFields(key, value);
//			if ("1".equals(processParam)) {
//				formInfo.addProcessParams(field, value);
//			}
		}
		return formInfo;
	}
	public static void main(String[] args) {
		System.err.println("0_reason".substring(0,2));
	}
	
	public static Map<String,Object> getFormInfo (HttpServletRequest request,String per) {
		
		// form_tablename_processPrama_field
		Map<String,Object>formParamMap = getParametersStartingWith(request,per);
		
		FormInfo formInfo = new FormInfo();
		
		for (Map.Entry<String, Object> entry : formParamMap.entrySet()) {
			
			
			String key = entry.getKey();
			Object value = entry.getValue();
			PropertyField propertyField = new PropertyField(key,value);
			
			// 先截取表名
			String tablename = key.split("_")[0];
			String processParam = key.split("_")[1];
			String field = key.substring((tablename+processParam).length()+2);	//流程字段名
			
			formInfo.setTablename(tablename);
			if ("1".equals(processParam)) {
				formInfo.addProcessParams(field, value);
			}
		}
		return formParamMap;
	}
	
	public static Map<String,Object> getFilterInfo (HttpServletRequest request) {
		Map<String,Object>filterParamMap = getParametersStartingWith(request,"filter_");
		// 分析参数Map,构造PropertyFilter列表
		for (Map.Entry<String, Object> entry : filterParamMap.entrySet()) {
		
			String filterName = entry.getKey();
			String value = (String) entry.getValue();
			// 如果value值为空,则忽略此filter.
			if (StringUtils.isNotBlank(value)) {
				PropertyFilter filter = new PropertyFilter(filterName, value);
			}
		}
		return filterParamMap;
	}
	
	/**
	 * 取得带相同前缀的Request Parameters.
	 * 
	 * 返回的结果Parameter名已去除前缀.
	 */
	public static Map<String,Object> getParametersStartingWith(HttpServletRequest request, String prefix) {
		
		Enumeration paramNames = request.getParameterNames();
		Map<String,Object>params = new TreeMap<String,Object>();
		if (prefix == null) {
			prefix = "";
		}
		while (paramNames != null && paramNames.hasMoreElements()) {
			String paramName = (String) paramNames.nextElement();
			if ("".equals(prefix) || paramName.startsWith(prefix)) {
				String unprefixed = paramName.substring(prefix.length());
				String[] values = request.getParameterValues(paramName);
				
				if (values == null || values.length == 0) {// NOSONAR
					// Do nothing, no values found at all.
				} else if (values.length > 1) {
					params.put(unprefixed, values);
				} else {
					params.put(unprefixed, values[0]);
				}
			}
		}
		return params;
	}
	
	
	
	
	
}
